package net.cyl.service.impl;

import lombok.extern.slf4j.Slf4j;
import net.cyl.controller.request.DomainAddRequest;
import net.cyl.interceptor.LoginInterceptor;
import net.cyl.manager.DomainManager;
import net.cyl.mapper.DomainMapper;
import net.cyl.model.DomainDO;
import net.cyl.service.DomainService;
import net.cyl.vo.DomainVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class DomainServiceImpl implements DomainService {
    @Autowired
    private DomainManager domainManager;

    @Override
    public List<DomainVO> listAllDomain() {

        Long accountNo = LoginInterceptor.loginUserThreadLocal.get().getAccountNo();
        List<DomainDO> customDomainList = domainManager.listCustomDomain(accountNo);
        List<DomainDO> officialDomainList = domainManager.listOfficialDomain();

        customDomainList.addAll(officialDomainList);
        return customDomainList.stream().map(this::convert).collect(Collectors.toList());
    }

    @Override
    public DomainVO createDomain(DomainAddRequest domainAddRequest) {
        Long accountNo = LoginInterceptor.loginUserThreadLocal.get().getAccountNo();
        DomainDO domainDO = new DomainDO();
        BeanUtils.copyProperties(domainAddRequest, domainDO);
        domainDO.setAccountNo(accountNo);
        domainManager.addDomain(domainDO);
        return convert(domainDO);
    }

    private DomainVO convert(DomainDO domainDO) {
        DomainVO domainVO = new DomainVO();
        BeanUtils.copyProperties(domainDO, domainVO);
        return domainVO;

    }
}
